/******************************************************下拉框*****************************************************/

_$.ComboBox=function (target){
    var _this = this;
    _$.ComboBox.superclass.constructor.call(_this,target);
};
_$.regClass("ComboBox",_$.ComboBox);

_$.extend(_$.ComboBox,_$.Combo, {
    _cls:_$._clsPre + "ComboBox",
    _uiCls:_$._uiPreCls + "ComboBox",
    data:[],
    _attrProps:function(){
        var _this = this;
        var properties = _$.ComboBox.superclass._attrProps.call(_this);
        properties.addAll(["apiServer","call","basePath","treeCall","idField","textField",'parentId','parentKey',"resultKey",{"params":"object","dataFilter":"function"}]);
        return properties;
    },
    _attrOpts:function(){
        var _this = this;
        var opts = _$.ComboBox.superclass._attrOpts.call(_this);
        return $.extend(true,opts,{
            "idField":"id",
            "textField":"name",
            "resultKey":"list",
            'parentId':'',
            "apiServer":"base",
            "call":"",
            "basePath":"/base",
            "parentKey":"parentId",
            "dataFilter":function(list){
                return list;
            }
        });
    },
    _initPanel:function(){
       var _this = this;
        _this._initData();
    },
    _init:function(){
        var _this = this;
        if(!_this._isEdit()){
            _this._initShow();
        }
    },
    _initData:function(){
        var _this = this;
        var parentId = _this.getOption("parentId");
        if(isEmpty(parentId)){
            _this._loadData(_this._editInit);
        }else{
            var parentId = _this.getOption("parentId");
            var parentObj = _$.getById(parentId);
            parentObj._on("onchange", function(e){
                var parentValue = parentObj.getValue();
                _this._loadTree(parentValue,_this._editInit);
            });
        }
    },
    _initShow:function(){
        var _this = this;
        var parentId = _this.getOption("parentId");
        if(isEmpty(parentId)){
            _this._loadData(_this._showInit);
        }else{
            var parentObj = _$.getById(parentId);
            var parentValue = parentObj.getValue();
            _this._loadTree(parentValue,_this._showInit);
        }
    },
    _loadData:function(func){
    },
    _loadTree:function(value,func){
    },
    _editInit:function(result){
        var _this = this;
        var state = result.getState();
        var info = result.getInfo();
        if(state == 0){
            var list = result.get("list");
            var dataFilter = _this.getOption("dataFilter");
            list = dataFilter.call(window,list);
            _this.data = list;
            _this._setData(list);

            var initValue= _this.options["initValue"];
            if(isNotEmpty(_this.value)){
                _this.setValue(_this.value);
            }else if(isNotEmpty(initValue)){
                _this.setValue(initValue);
            }else{
                _this.value = null;
                _this.combo_input.html("");
            }
            _this._loadSuccess();
        }else{
            $.cc.error(info);
        }
    },
    _showInit:function(result){
        var _this = this;
        var state = result.getState();
        var info = result.getInfo();
        if(state == 0){
            var list = result.get("list");
            var dataFilter = _this.getOption("dataFilter");
            list = dataFilter.call(window,list);

            _this.data = list;
            var initValue = _this.getOption("initValue");
            if(isNotEmpty(_this.value)){
                _this.setValue(_this.value);
            }else if (isNotEmpty(initValue)){
                _this.setValue(initValue);
            }
            _this._loadSuccess();
        }else{
            $.cc.error(info);
        }
    },
    _searchByTxt:function(searchTxt){
        var _this = this;
        var multiple = _this.getOption("multiple");
        var textField = _this.getOption("textField");
        if(isEmpty(searchTxt)){
            _this._setData(_this.data);
        }else{
            var list = [];
            for(var i=0;i<_this.data.length;i++){
                var item = _this.data[i];
                if(item[textField].indexOf(searchTxt) != -1){
                    list.push(item);
                }
            }
            _this._setData(list);
        }
        _this.setValue(_this.value,false);
        _this.index = -1;
    },
    _setData:function(list){
        var _this = this;
        var multiple = _this.getOption("multiple");
        var idField = _this.getOption("idField");
        var textField = _this.getOption("textField");

        $(".combobox_item",_this.panel_content).unbind();
        _this.panel_content.empty();

        if(list != null && list.size()>0){
            list.each(function(item,i){
                var id = item[idField];
                var name = item[textField];
                var _html = '<p class="combobox_item" data-value="'+id+'">'+name+'</p>';
                _this.panel_content.append(_html);
            });
        }
        if(!multiple){
            _this._bindSingleItemEvents();
        }else{
            _this._bindMultiIetmEvents();
        }
        _this._bindPanelEvents();
    },
    _bindPanelEvents:function(){
        var _this = this;
        var opts = _$.ComboBox.superclass._bindPanelEvents.call(_this);
        _this.index = -1;
        _this.panel.unbind('keyup').bind("keyup",function(e){
            var $combobox_items = $(".combobox_item",_this.panel_content);
            var len = $combobox_items.size();
            var panelHeight = _this.getOption("panelHeight");
            var cnt = (panelHeight % 50 == 0) ? panelHeight/50 : Math.floor(panelHeight/50) + 1;

            var code = event.keyCode;
            if(code == 38){
                if (_this.index<=0) {
                    _this.index = len;
                }
                _this.index--;
                var $combobox_item = $combobox_items.eq(_this.index);
                $combobox_item.addClass("combo_selected").siblings().removeClass("combo_selected");

                _this.panel_content.animate({scrollTop:(_this.index -cnt)*25},100);
            }else if(code == 40){
                _this.index++;
                if (_this.index>=len) {
                    _this.index = 0;
                }

                var $combobox_item = $combobox_items.eq(_this.index);
                $combobox_item.addClass("combo_selected").siblings().removeClass("combo_selected");

                _this.panel_content.animate({scrollTop:(_this.index -cnt)*25},100);
            }else if(code == 13){
                var val =  $combobox_items.eq(_this.index).attr("data-value");
                _this.setValue(val);
                hideComboPanel(_this.id);
            }
            return false;
        });
    },
    _bindSingleItemEvents:function(){
        var _this = this;
        $(".combobox_item",_this.panel_content).unbind("click").bind("click",function(){
            var val = $(this).attr("data-value");
            _this.setValue(val);
            hideComboPanel(_this.id);
            return false;
        });
    },
    _bindMultiIetmEvents:function(){
        var _this = this;
        $(".combobox_item",_this.panel_content).unbind("click").bind("click",function(){
            var val = $(this).attr("data-value");
            var value = _this.getValue();
            var values = [];
            if(isNotEmpty(value)){
                values = (value+"").split(",");
            }

            if(values.indexOf(val) == -1){
                values.add(val);
            }else{
                values.remove(val);
            }
            _this.setValue(values.join(","));
            return false;
        });
    },
    _clearPanel:function(){
        var _this = this;
        if( _this.panel_content){
            _this.panel_content.unbind();
            _this.panel_content.remove();
        }
        if( _this.panel_search){
            _this.panel_search.unbind();
            _this.panel_search.remove();
        }

        if( _this.panel){
            _this.panel.unbind();
            _this.panel.remove();
        }
    },
    setValue:function(value,change) {
        var _this = this;
        if(isEmpty(change)){
            change = true;
        }

        var idField = _this.getOption("idField");
        var textField = _this.getOption("textField");
        var multiple = _this.getOption("multiple");

        if(isEmpty(value) || isEmpty(_this.data) || _this.data.length == 0){
            _this.value = null;
            if (_this._isEdit()) {
                _this.combo_input.html("");
            } else {
                _this.component.html("");
            }
        }else{
            _this.value = value;
            if (_this._isEdit()) {
                if(!multiple){
                    $(".combobox_item",_this.panel_content).each(function(){
                        var val = $(this).attr("data-value");
                        $(this).removeClass("combobox_item_selected");
                    });


                    var flag = _this.data.some(function(item,i){
                        var id = item[idField];
                        var name = item[textField];

                        if(id == value){
                            $(".combobox_item[data-value='"+value+"']",_this.panel_content).each(function(){
                                $(this).addClass("combobox_item_selected");
                            });
                            _this.combo_input.html(name);
                            return true;
                        }
                        return false;
                    });

                    if(!flag){
                        _this.value = null;
                        _this.combo_input.html("");
                    }
                }else{
                    var values = (value+"").split(",");
                    if(values != null && values.length>0){
                        var names=[] ;
                        for(var i=0;i<values.length;i++){
                            var itemValue = values[i];

                            _this.data.each(function(item,i){
                                var id = item[idField];
                                var name = item[textField];
                                if(id == itemValue){
                                    names.add(name);
                                }
                            });
                        }
                        if(names.length>0){
                            _this.combo_input.html(names.join(","));
                        }else{
                            _this.value = null;
                            _this.combo_input.html('');
                        }

                        var arr = _this.data.filter(function(item,index){
                            for(var i=0;i<values.length;i++){
                                if(item[idField] == values[i]){
                                    return false;
                                }
                            }
                            return true;
                        });
                        _this._setData(arr);
                    }else{
                        _this.value = null;
                        _this.combo_input.html('');
                    }
                }
            } else {
                if(!multiple){
                    var flag = _this.data.some(function(item,i){
                        var id = item[idField];
                        var name = item[textField];

                        if(id == value){
                            _this.component.html(name);
                            return true;
                        }
                        return false;
                    });
                    if(!flag){
                        _this.value = null;
                        _this.component.html("");
                    }
                }else{
                    var values = (value+"").split(",");
                    if(values.length>0){
                        var names=[] ;
                        for(var i=0;i<values.length;i++){
                            var itemValue = values[i];

                            _this.data.each(function(item,i){
                                var id = item[idField];
                                var name = item[textField];
                                if(id == itemValue){
                                    names.add(name);
                                }
                            });
                        }
                        if(names.length>0){
                            _this.component.html(names.join(","));
                        }else{
                            _this.value = null;
                            _this.component.html('');
                        }
                    }else{
                        _this.value = null;
                        _this.component.html('');
                    }
                }
            }
        }
        if(change){
            _this._fire("onchange");
        }
    },
    _hideMore:function(){
        var _this = this;
        var multiple = _this.getOption("multiple");
        if(multiple){
            _this.show_more_bar.addClass("icon_right").removeClass("icon_left");
            _this.show_more.empty();
            _this.show_more.hide();
        }
    },
    _showMore:function(){
        var _this = this;
        var multiple = _this.getOption("multiple");
        var idField = _this.getOption("idField");
        var textField = _this.getOption("textField");
        if(multiple){
            hideComboPanel(_this.id);
            _this.show_more_bar.addClass("icon_left").removeClass("icon_right");
            _this.show_more.empty();
            _this.show_more.show();

            if(isNotEmpty(_this.value)){
                var values = (_this.value+"").split(",");
                var names = [];
                values.each(function(val){
                    _this.data.each(function(item){
                        if(val == item[idField]){
                            names.add(item[textField]);
                        }
                    });
                });

                if(values.length>0){
                    for(var i=0;i<values.length;i++){
                        var id = values[i];
                        var name = names[i];
                        var _html='<div class="data_item" ><span>'+name+'</span><i class="deleteicon" data-value="'+id+'"></i></div>';
                        _this.show_more.append(_html);
                    }

                    $(".data_item .deleteicon",_this.show_more).click(function(){
                        var val = $(this).attr("data-value");
                        if(values.indexOf(val) != -1){
                            values.remove(val);
                            _this.setValue(values.join(","));
                            var item = $(this).closest(".data_item");
                            $(this).unbind();
                            item.remove();
                        }
                        return false;
                    });
                }
            }
        }
    },
    getValue:function(){
        var _this = this;
        return _this.value;
    }
});
